import { resolve } from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import svgLoader from 'vite-svg-loader';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ArcoResolver } from 'unplugin-vue-components/resolvers';
import { vitePluginForArco } from '@arco-plugins/vite-vue';
import configArcoStyleImportPlugin from './plugin/arcoStyleImport';

const getBaseConfig = (theme: string) => {
  return defineConfig({
    // define: {
    //   __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: true,
    // },
    base: './',
    plugins: [
      vue(),
      vueJsx(),
      // 按需加载
      AutoImport({
        resolvers: [ArcoResolver()],
      }),
      // 按需加载
      Components({
        resolvers: [
          ArcoResolver({
            sideEffect: true,
          }),
        ],
      }),
      // 按需加载
      vitePluginForArco({
        style: 'css',
      }),
      svgLoader({ svgoConfig: {} }),
      configArcoStyleImportPlugin(theme),
    ],
    server: {
      hmr: true,
      proxy: {
        '/elcapi': {
          target: 'https://elcapi-test.zrkj0627.com/elcapi',
          // target: 'http://localhost:12557/nhapi',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/elcapi/, ''),
        },
      },
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, '../src'),
        },
        {
          find: '@mc',
          replacement: resolve(
            __dirname,
            '../src/sub-modules/medicine-cabinet'
          ),
        },
        {
          find: 'assets',
          replacement: resolve(__dirname, '../src/assets'),
        },
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js', // Resolve the i18n warning issue
        },
        {
          find: 'vue',
          replacement: 'vue/dist/vue.esm-bundler.js', // compile template
        },
      ],
      extensions: ['.ts', '.js', '.vue'],
    },
    define: {
      'process.env': {},
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${resolve(
              'src/assets/style/breakpoint.less'
            )}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
    optimizeDeps: {
      exclude: ['src/examples'], // 排除依赖预构建
    },
  });
};

export default getBaseConfig;
